/*
 * generate by table-builder.Don't modify it
 */
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay;
using System.Collections.Generic;


namespace CfgTable
{
    public partial class TbCfgPlayerParam
    {
        private readonly Dictionary<int, CfgPlayerParam> _dataMap;
        private readonly List<CfgPlayerParam> _dataList;


        public TbCfgPlayerParam(ByteBuf _buf)
        {
            //first read common data
            _TbCommonPlayerParam _commonData = null;
            var commonSize = _buf.ReadSize();
            if (commonSize > 0)
            {
                _commonData = new _TbCommonPlayerParam(_buf);
            }

            //then read row data
            var size = _buf.ReadSize();
            _dataMap = new Dictionary<int, CfgPlayerParam>(size * 3 / 2);
            _dataList = new List<CfgPlayerParam>(size);

            for (int i = 0; i < size; i++)
            {
                CfgPlayerParam _v;
                _v = CfgPlayerParam.DeserializeCfgPlayerParam(_buf, _commonData);
                _dataList.Add(_v);
                _dataMap.Add(_v.Id, _v);
            }

            // read extra fields 

            PostInit();
        }

        public IReadOnlyDictionary<int, CfgPlayerParam> DataMap => _dataMap;
        public IReadOnlyList<CfgPlayerParam> DataList => _dataList;

        public CfgPlayerParam Get(int key)
        {
            if (_dataMap.TryGetValue(key, out var v))
            {
                return v;
            }
            IQIGame.Onigao.Framework.LogGame.LogError("[TbCfgPlayerParam] config not found,id:" + key.ToString());

            return null;
        }

        public CfgPlayerParam GetWithoutError(int key) => _dataMap.TryGetValue(key, out var v) ? v : null;

        /// <summary>
        /// table data file
        /// </summary>
        public static string TableFileName { get; } = "playerparam";

        /// <summary>
        /// load table function
        /// </summary>
        public static TbCfgPlayerParam LoadFromFunc(System.Func<string, ByteBuf> loadFunc)
        {
            return new TbCfgPlayerParam(loadFunc(TableFileName));
        }

        /// <summary>
        /// post process table
        /// </summary>
        partial void PostInit();

    }

    public partial class CfgPlayerParam
    {
        private CfgPlayerParam(ByteBuf _buf, _TbCommonPlayerParam _commonData)
        {
            Id = _buf.ReadInt();
            Value = _buf.ReadInt();

            PostInit();
        }

        internal static CfgPlayerParam DeserializeCfgPlayerParam(ByteBuf _buf, _TbCommonPlayerParam _commonData)
        {
            return new CfgPlayerParam(_buf, _commonData);
        }

        /// <summary>
        /// major key
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// 参数初始值
        /// </summary>
        public int Value { get; private set; }


        /// <summary>
        /// post process table
        /// </summary>
        partial void PostInit();
    }

    /// <summary>
    /// internal common data.Optimize memory
    /// </summary>
    internal class _TbCommonPlayerParam
    {

        internal _TbCommonPlayerParam(ByteBuf _buf)
        {
        }

    }
}
